package cheme.ProcessModel.Interpreter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.Interpreter.IRule.RuleResult;
import cheme.ProcessModel.Visitors.IModelVisitor;

public interface IRuleCheckVisitor extends IModelVisitor
{
	void add(IRule rule);
	
	IRule.RuleResult check(IModelElement object);
	
	List<IRule> getRules(); // PA Change
		
	public class RuleCheckVisitorStub extends IModelVisitor.ModelVisitorStub implements IRuleCheckVisitor
	{
		private List<IRule> rules = null;
		
		public void add(IRule rule)
		{
			if(null == rules)
			{
				rules = new ArrayList<IRule>();
			}
			
			rules.add(rule);
		}

		public RuleResult check(IModelElement object)
		{
			RuleResult result = null;
			
			if(null != rules)
			{
				Iterator<IRule> itr = rules.iterator();
				IRule current = null;
				
				/*
				 * break out on the first rule failure
				 */
				while(itr.hasNext() && !(result instanceof IRule.Failure || result instanceof IRule.Warning))
				{
					current = itr.next();
					result = current.applyTo(object);
				}
			}
			else
			{
				result = new IRule.Success(object);
			}
			
			return result;
		}

		//PA change
		public List<IRule> getRules() {
			// TODO Auto-generated method stub
			return rules;
		}
	}
}
